<?php
/**
 * User: @lixu
 * Date: 2019-05-29
 * Time: 17:33
 */

namespace Biz\service\wiki;

use Biz\service\esSearch\Wiki as esWiki;

class TagList
{
    const TAB_TYPE = 'tag_list';

    public static function getList($tag, $params, $sortType, $page, $uid)
    {

        // 通过tag params调用es接口

        $params = self::verifyParams($params);

        $where = [
            'brand_id' => isset($params['brand']) ? $params['brand'] : 0,
            'tags' => $tag,
            'price_range' => isset($params['price_range']) ? $params['price_range'] : 0
        ];
        $where = array_filter($where);

        $result = esWiki::getList($where, esWiki::SCENES_APP_TAG_LIST, $sortType, $page);

        $list = Wiki::getCompleteWikiList($result['rows']);
        $list = $list ? array_values($list) : [];
        return ['is_over' => $result['is_over'], 'list' => $list];
    }

    private static function verifyParams($params)
    {
        $result = [];
        if($params) {
            foreach ($params as $param) {
                if ($param['type'] == 'price_range') {
                    $result['price_range'] = array_column($param['list'], 'title');
                    sort($result['price_range']);
                } else if ($param['type'] == 'brand') {
                    $result['brand'] = array_column($param['list'], 'id');
                }
            }
        }
        return $result;
    }
}